package com.jblapp.icu.ui;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.jblapp.icu.R;
import com.jblapp.icu.http.Register;

public class RegisterActivity extends Activity {
	private EditText num = null;
	private EditText pwd = null;
	private EditText confirmPwd = null;
	private Button regNow = null;
	private String password = null;
	private String confirmPassword = null;
	private String phoneNum = null;
	private ProgressDialog progress=null;
	private AlertDialog.Builder dialog = null;
	private static String TAG = "RegisterActivity";
	private final static int ONE=1;
	private final static int TWO=2;
	private final static int THREE=3;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.register);
		num = (EditText) findViewById(R.id.reg_num);
		pwd = (EditText) findViewById(R.id.reg_pwd);
		confirmPwd = (EditText) findViewById(R.id.reg_confirm_pwd);
		regNow = (Button) findViewById(R.id.reg_reg_now);
		regNow.setOnClickListener(new MOnClickListener(this));
		progress=new ProgressDialog(this);
	}

	@Override
	protected void onStart() {
		super.onStart();
	}

	private class MOnClickListener implements OnClickListener {
		private Context context = null;

		public MOnClickListener(Context context) {
			this.context = context;
		}

		@Override
		public void onClick(View v) {
			confirmPassword = confirmPwd.getText().toString().trim();
			password = pwd.getText().toString().trim();
			phoneNum = num.getText().toString().trim();
			Log.e(TAG, "password:" + password + "\nconfirmPassword:"
					+ confirmPassword + "\nphoneNum:" + phoneNum);
			switch (v.getId()) {
			case R.id.reg_reg_now:
				// This phone number has signed in
				if ("".equals(phoneNum) || phoneNum.length() != 11) {
					dialog = new AlertDialog.Builder(context);
					dialog.setTitle(R.string.Tip);
					dialog.setMessage(R.string.EmptyPhoneNum);
					dialog.setNegativeButton(R.string.back, null);
					dialog.show();
					return;
				} else if (isRegistered(phoneNum)) {
					// Check where the phone number is registered!
					dialog = new AlertDialog.Builder(context);
					dialog.setTitle(R.string.Tip);
					dialog.setMessage(R.string.ErrorPhoneNumber);
					dialog.setNegativeButton(R.string.back, null);
					dialog.show();
					return;
				}
				// Password Error
				if ("".equals(password) || "".equals(confirmPassword)) {
					// Toast.makeText(context,
					// getString(R.string.EmptyPassword), Toast.LENGTH_LONG)
					// .show();
					dialog = new AlertDialog.Builder(context);
					dialog.setTitle(R.string.Tip);
					dialog.setMessage(R.string.EmptyPassword);
					dialog.setNegativeButton(R.string.back, null);
					dialog.show();
					return;
				} else if ((password.length() != confirmPassword.length())
						|| !password.equals(confirmPassword)) {
					// Toast.makeText(context, "Error Password",
					// Toast.LENGTH_LONG)
					// .show();
					dialog = new AlertDialog.Builder(context);
					dialog.setTitle(R.string.Tip);
					dialog.setMessage(R.string.ErrorPassword);
					dialog.setNegativeButton(R.string.back, null);
					dialog.show();
					return;
				}
				progress.setTitle("Write");
				progress.setMessage("Loading");
				progress.setCancelable(false);
//				progress.show();
				List<NameValuePair> params =new ArrayList<NameValuePair>();
				 params.add(new BasicNameValuePair("flag", "1"));
				 params.add(new BasicNameValuePair("mobilephone", "+86"+phoneNum));
				 params.add(new BasicNameValuePair("password",
				 "14e1b600b1fd579f47433b88e8d85291"));
				 params.add(new BasicNameValuePair("current_longitude", "39915000"));
				 params.add(new BasicNameValuePair("current_latitude", "116404000"));
				 params.add(new BasicNameValuePair("reg_location", "+86"));
				 progress.show();
				 new Thread(new Register(msgHandler, params)).start();
				/**
				new Thread(){
					@Override
					public void run() {
						
						List<NameValuePair> params = new ArrayList<NameValuePair>();
						params.add(new BasicNameValuePair("flag", "1"));
						params.add(new BasicNameValuePair("mobilephone",
								"+86" + phoneNum));
						params.add(new BasicNameValuePair("password",
								"14e1b600b1fd579f47433b88e8d85291"));
						params.add(new BasicNameValuePair("current_longitude",
								"39915000"));
						params.add(new BasicNameValuePair("current_latitude",
								"116404000"));
						params.add(new BasicNameValuePair("reg_location", "+86"));
						Register register=new Register(params);
						try {
							register.regStatus();
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				}.start();
				**/
//				Intent reg = new Intent(context, RegisterConfirmActivity.class);
//				reg.putExtra("regNum", "+86" + phoneNum);
//				reg.putExtra("regPwd", password);
//				startActivity(reg);
				break;
			default:
				break;
			}
		}
	}

	public boolean isRegistered(String phoneNum2) {
		return false;
	}

	private Handler msgHandler = new Handler() {

		@Override
		public void handleMessage(Message msg) {
//			Toast.makeText(RegisterActivity.this, msg.what, Toast.LENGTH_LONG)
//					.show();
			switch (msg.what) {
			case RegisterActivity.ONE:
				progress.cancel();
				break;
			case RegisterActivity.TWO:
				progress.cancel();
				break;
			case RegisterActivity.THREE:
				progress.cancel();
				break;
			default:
				break;
			}
		}
	};
}
